package com.xk.meeting.service;

import com.xk.meeting.dao.MeetingDaoImpl;
import com.xk.meeting.dao.MessageDao;
import com.xk.meeting.dao.MessageDaoImpl;
import com.xk.meeting.entity.Message;
import com.xk.meeting.entity.User;
import com.xk.meeting.entityutils.MessageBean;
import com.xk.meeting.utils.UtilHelper;

import java.util.ArrayList;
import java.util.List;

public class MessageServiceImpl implements MessageService{
    MessageDao messageDao = new MessageDaoImpl();
    @Override
    public void addMessage(Message message) {
        messageDao.addMessage(message);
    }

    @Override
    public boolean isUnready(int uid) {
        Message message = messageDao.getUnready(uid);
        if (message != null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public List<MessageBean> getMessageBean(User user) {
        List<MessageBean> lists = new ArrayList<>();
        MessageBean messageBean = null;
        List<Message> messages = messageDao.getMessages(user);
        for (Message message : messages){
            messageBean = new MessageBean();
            messageBean.setMessage(message);
            switch (message.getStatus()){
                case 0:{
                    messageBean.setStatus("未读");
                    messageBean.setStyle("unread");
                    break;
                }
                case 1:{
                    messageBean.setStatus("已读");
                    messageBean.setStyle("read");
                    break;
                }
            }
            messageBean.setSendtime(UtilHelper.dateToString(message.getSendtime()));
            lists.add(messageBean);
        }
        return lists;
    }

    @Override
    public void updateStatus(int id) {
        messageDao.updateStatus(id);
    }

    @Override
    public int delMessage(int id) {
        return messageDao.delMessage(id);
    }

}
